<template>
  <div class="customer-service-report">
    <el-card class="report-filter">
      <el-form :inline="true" :model="filterForm" class="filter-form">
        <el-form-item label="开始时间">
          <el-date-picker
            v-model="filterForm.startTime"
            type="date"
            placeholder="选择开始日期"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间">
          <el-date-picker
            v-model="filterForm.endTime"
            type="date"
            placeholder="选择结束日期"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchReportData">查询</el-button>
          <el-button @click="resetFilter">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <el-card class="report-table">
      <el-table
        :data="reportData"
        style="width: 100%"
        v-loading="loading"
        border>
        <el-table-column
          prop="serviceId"
          label="客服ID"
          width="100">
        </el-table-column>
        <el-table-column
          prop="serviceNickname"
          label="客服昵称"
          width="150">
        </el-table-column>
        <el-table-column
          prop="handleOrderCount"
          label="处理订单数"
          width="120">
        </el-table-column>
        <el-table-column
          prop="handleAfterSalesCount"
          label="处理售后数"
          width="120">
        </el-table-column>
        <el-table-column
          prop="avgHandleTime"
          label="平均处理时间(分钟)"
          width="150">
          <template #default="scope">
            {{ scope.row.avgHandleTime !== null ? scope.row.avgHandleTime.toFixed(2) : 'N/A' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="lastWorkTime"
          label="最后工作时间"
          width="180">
          <template #default="scope">
            {{ scope.row.lastWorkTime ? formatDate(scope.row.lastWorkTime) : '' }}
          </template>
        </el-table-column>
        <el-table-column
          prop="entryTime"
          label="入职时间"
          width="180">
          <template #default="scope">
            {{ scope.row.entryTime ? formatDate(scope.row.entryTime) : '' }}
          </template>
        </el-table-column>
      </el-table>
      
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
        class="pagination">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import adminApi from '@/http/adminApi';

export default {
  name: 'CustomerServiceReport',
  data() {
    return {
      loading: false,
      filterForm: {
        startTime: '',
        endTime: ''
      },
      reportData: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
    };
  },
  
  mounted() {
    this.fetchReportData();
  },
  
  methods: {
    async fetchReportData() {
      this.loading = true;
      try {
        const params = {
          page: this.pagination.currentPage,
          pageSize: this.pagination.pageSize,
          startTime: this.filterForm.startTime,
          endTime: this.filterForm.endTime
        };
        
        const response = await adminApi.getCustomerServiceReportData(params);
        if (response.code === 200) {
          this.reportData = response.data.list || [];
          this.pagination.total = response.data.total || 0;
        } else {
          this.$message.error(response.message || '获取数据失败');
        }
      } catch (error) {
        this.$message.error('获取数据时发生错误');
        console.error(error);
      } finally {
        this.loading = false;
      }
    },
    
    resetFilter() {
      this.filterForm = {
        startTime: '',
        endTime: ''
      };
      this.pagination.currentPage = 1;
      this.fetchReportData();
    },
    
    handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.pagination.currentPage = 1;
      this.fetchReportData();
    },
    
    handleCurrentChange(val) {
      this.pagination.currentPage = val;
      this.fetchReportData();
    },
    
    formatDate(date) {
      if (!date) return '';
      const d = new Date(date);
      return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}`;
    }
  }
};
</script>

<style scoped>
.customer-service-report {
  padding: 20px;
}

.report-filter {
  margin-bottom: 20px;
}

.filter-form {
  display: flex;
  flex-wrap: wrap;
}

.report-table {
  margin-top: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}
</style>